﻿<UserControl x:Class="tomenglertde.ResXManager.View.Visuals.TranslationsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="1000"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:converters="clr-namespace:tomenglertde.ResXManager.View.Converters"
             xmlns:properties="clr-namespace:tomenglertde.ResXManager.View.Properties"
             xmlns:visuals="clr-namespace:tomenglertde.ResXManager.View.Visuals"
             xmlns:toms="urn:TomsToolbox"
             xmlns:infrastructure="urn:ResXManager.Infrastructure"
             xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             xmlns:styles="urn:ResXManager.Styles"
             x:Name="UserControl"
             d:DataContext="{d:DesignInstance visuals:TranslationsViewModel}" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
  <UserControl.Resources>
    <DataTemplate DataType="{x:Type infrastructure:CultureKey}">
      <DockPanel>
        <Image DockPanel.Dock="Left" x:Name="image" Stretch="None" Source="{Binding Path=Culture, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" />
        <Decorator x:Name="padding" DockPanel.Dock="Left" Width="4" />
        <TextBlock Text="{Binding Path=Culture.DisplayName, FallbackValue=Neutral}" TextTrimming="CharacterEllipsis" />
      </DockPanel>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Culture}" Value="{x:Null}">
          <Setter TargetName="image" Property="Source" Value="{Binding Path=DataContext.Configuration.NeutralResourcesLanguage, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" />
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </UserControl.Resources>
  <DockPanel Margin="10" Grid.IsSharedSizeScope="True">
    <DockPanel.Resources>
      <Style TargetType="Button" x:Key="PushButtonStyle" BasedOn="{StaticResource {x:Static styles:ResourceKeys.ButtonStyle}}">
        <Setter Property="Margin" Value="5" />
        <Setter Property="Padding" Value="15,3" />
      </Style>
    </DockPanel.Resources>
    <DockPanel DockPanel.Dock="Bottom">
      <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" VerticalAlignment="Bottom">
        <StackPanel.Resources>
          <Style TargetType="Button" BasedOn="{StaticResource PushButtonStyle}" />
        </StackPanel.Resources>
        <Button Command="{Binding ApplyAllCommand}">
          <ContentControl Content="{x:Static properties:Resources.Translations_ApplyAll}" Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly=toms:ResourceKeys, ResourceId={x:Static toms:ResourceKeys.SharedWidthContainerStyleKeyName}}}" HorizontalContentAlignment="Center" />
        </Button>
        <Button Command="{Binding ApplySelectedCommand}">
          <ContentControl Content="{x:Static properties:Resources.Translations_ApplySelected}" Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly=toms:ResourceKeys, ResourceId={x:Static toms:ResourceKeys.SharedWidthContainerStyleKeyName}}}" HorizontalContentAlignment="Center" />
        </Button>
        <Button Command="{Binding RestartCommand}">
          <ContentControl Content="{x:Static properties:Resources.Translations_Restart}" Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly=toms:ResourceKeys, ResourceId={x:Static toms:ResourceKeys.SharedWidthContainerStyleKeyName}}}" HorizontalContentAlignment="Center" />
        </Button>
        <Button Command="{Binding StopCommand}">
          <ContentControl Content="{x:Static properties:Resources.Translations_Stop}" Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly=toms:ResourceKeys, ResourceId={x:Static toms:ResourceKeys.SharedWidthContainerStyleKeyName}}}" HorizontalContentAlignment="Center" />
        </Button>
      </StackPanel>
      <ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="100" HorizontalScrollBarVisibility="Auto">
        <ItemsControl ItemsSource="{Binding TranslationSession.Messages}" Margin="5" Focusable="False" />
      </ScrollViewer>
    </DockPanel>
    <Decorator Height="10" DockPanel.Dock="Bottom" />
    <Grid>
      <DockPanel>
        <visuals:TranslatorConfiguration DockPanel.Dock="Top" IsEnabled="{Binding TranslationSession.IsComplete}" Translators="{Binding Translators}" />
        <Decorator Height="10" DockPanel.Dock="Top" />
        <DataGrid ItemsSource="{Binding Items}" Style="{DynamicResource {x:Static styles:ResourceKeys.DataGridStyle}}"
                  AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False"
                  IsEnabled="{Binding TranslationSession.IsComplete}" toms:MultiSelectorExtensions.SelectionBinding="{Binding SelectedItems}">
          <DataGrid.RowHeaderTemplate>
            <DataTemplate DataType="DataGridRowHeader">
              <CheckBox Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly=toms:ResourceKeys, ResourceId={x:Static toms:ResourceKeys.DataGridRowCheckBoxStyleKeyName}}}" />
            </DataTemplate>
          </DataGrid.RowHeaderTemplate>
          <DataGrid.Columns>
            <DataGridTextColumn Width="2*" Binding="{Binding Source}" IsReadOnly="True">
              <DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                  <Setter Property="ToolTip" Value="{Binding Entry.Key}" />
                </Style>
              </DataGridTextColumn.ElementStyle>
              <DataGridTextColumn.HeaderTemplate>
                <DataTemplate>
                  <StackPanel Orientation="Horizontal" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridCellsPanel}}" d:DataContext="{d:DesignInstance visuals:TranslationsViewModel}">
                    <TextBlock Text="{x:Static properties:Resources.Translations_Source}" VerticalAlignment="Center" />
                    <Decorator Width="5" />
                    <ComboBox Width="150" ItemsSource="{Binding ResourceManager.Cultures}"
                              SelectedItem="{Binding SourceCulture}" Style="{DynamicResource {x:Static styles:ResourceKeys.ComboBoxStyle}}" />
                  </StackPanel>
                </DataTemplate>
              </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>
            <DataGridTemplateColumn Width="24" SortMemberPath="TargetCulture" IsReadOnly="True" CanUserResize="False">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate DataType="{x:Type visuals:TranslationItem}">
                  <Grid>
                    <Image Stretch="None" Source="{Binding Path=TargetCulture.Culture, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" HorizontalAlignment="Center">
                      <Image.ToolTip>
                        <TextBlock Text="{Binding Path=TargetCulture.Culture.DisplayName, FallbackValue=Neutral}" />
                      </Image.ToolTip>
                    </Image>
                  </Grid>
                </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="3*" IsReadOnly="True">
              <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                  <DockPanel DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridCellsPanel}}" d:DataContext="{d:DesignInstance visuals:TranslationsViewModel}">
                    <StackPanel DockPanel.Dock="Right" VerticalAlignment="Center" Orientation="Horizontal">
                      <Decorator Width="20" />
                      <CheckBox Content="{x:Static properties:Resources.TranslationsPrefixHeader}"
                                IsChecked="{Binding Configuration.PrefixTranslations}"
                                Style="{DynamicResource {x:Static styles:ResourceKeys.CheckBoxStyle}}" />
                      <StackPanel Orientation="Horizontal" MinWidth="40">
                        <TextBlock Text="&quot;" VerticalAlignment="Center" />
                        <TextBox Text="{Binding Configuration.TranslationPrefix}" BorderThickness="0" Padding="-1" Background="Transparent" Style="{DynamicResource {x:Static styles:ResourceKeys.TextBoxStyle}}" VerticalAlignment="Center" />
                        <TextBlock Text="&quot;" VerticalAlignment="Center" />
                      </StackPanel>
                    </StackPanel>
                    <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
                      <TextBlock Text="{x:Static properties:Resources.Translations_Targets}" VerticalAlignment="Center" />
                      <Decorator Width="5" />
                      <ToggleButton x:Name="TargetCulturesButton" Style="{DynamicResource {x:Static styles:ResourceKeys.ToggleButtonStyle}}">
                        <ToggleButton.Resources>
                          <CollectionViewSource x:Key="SelectedTargetCulturesSource" Source="{Binding  SelectedTargetCultures}">
                            <CollectionViewSource.SortDescriptions>
                              <componentModel:SortDescription PropertyName="." />
                            </CollectionViewSource.SortDescriptions>
                          </CollectionViewSource>
                        </ToggleButton.Resources>
                        <ItemsControl ItemsSource="{Binding Source={StaticResource SelectedTargetCulturesSource}}" VerticalAlignment="Center">
                          <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                              <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                          </ItemsControl.ItemsPanel>
                          <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="infrastructure:CultureKey">
                              <Image x:Name="image" Source="{Binding Culture, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" Stretch="None" Margin="2,4" />
                              <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Culture}" Value="{x:Null}">
                                  <Setter TargetName="image" Property="Source" Value="{Binding Path=DataContext.Configuration.NeutralResourcesLanguage, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" />
                                </DataTrigger>
                              </DataTemplate.Triggers>
                            </DataTemplate>
                          </ItemsControl.ItemTemplate>
                        </ItemsControl>
                      </ToggleButton>
                      <Popup IsOpen="{Binding Path=IsChecked, ElementName=TargetCulturesButton, Mode=OneWay}"
                             StaysOpen="False"
                             Placement="Bottom" PlacementTarget="{Binding ElementName=TargetCulturesButton}">
                        <i:Interaction.Behaviors>
                          <toms:PopupFocusManagerBehavior ToggleButton="{Binding ElementName=TargetCulturesButton}" />
                        </i:Interaction.Behaviors>
                        <Border Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                          <ListBox ItemsSource="{Binding AllTargetCultures}"
                                   Style="{DynamicResource {x:Static styles:ResourceKeys.ListBoxStyle}}"
                                   toms:MultiSelectorExtensions.SelectionBinding="{Binding SelectedTargetCultures}"
                                   SelectionMode="Multiple" KeyboardNavigation.TabNavigation="Once">
                            <ListBox.ItemTemplate>
                              <DataTemplate DataType="infrastructure:CultureKey">
                                <DockPanel Margin="2">
                                  <CheckBox DockPanel.Dock="Left"
                                            IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}"
                                            Style="{DynamicResource {x:Static styles:ResourceKeys.CheckBoxStyle}}" />
                                  <Decorator DockPanel.Dock="Left" Width="3" />
                                  <Image x:Name="image" DockPanel.Dock="Left"
                                         Source="{Binding Culture, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" />
                                  <Decorator DockPanel.Dock="Left" Width="3" />
                                  <Decorator DockPanel.Dock="Right" Width="3" />
                                  <ContentPresenter x:Name="content" Content="{Binding Culture.DisplayName}" VerticalAlignment="Center" />
                                </DockPanel>
                                <DataTemplate.Triggers>
                                  <DataTrigger Binding="{Binding Culture}" Value="{x:Null}">
                                    <Setter TargetName="image" Property="Source" Value="{Binding Path=DataContext.Configuration.NeutralResourcesLanguage, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter={x:Static converters:CultureToImageSourceConverter.Default}}" />
                                    <Setter TargetName="content" Property="Content" Value="{Binding Path=DataContext.Configuration.NeutralResourcesLanguage.DisplayName, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />
                                  </DataTrigger>
                                </DataTemplate.Triggers>
                              </DataTemplate>
                            </ListBox.ItemTemplate>
                            <ListBox.ItemContainerStyle>
                              <Style TargetType="ListBoxItem">
                                <Setter Property="Template">
                                  <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                      <ContentPresenter />
                                    </ControlTemplate>
                                  </Setter.Value>
                                </Setter>
                              </Style>
                            </ListBox.ItemContainerStyle>
                          </ListBox>
                        </Border>
                      </Popup>
                    </StackPanel>
                  </DockPanel>
                </DataTemplate>
              </DataGridTemplateColumn.HeaderTemplate>
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate DataType="visuals:TranslationItem">
                  <ComboBox Margin="0" Padding="0" BorderThickness="0" DisplayMemberPath="TranslatedText"
                            ItemsSource="{Binding OrderedResults}" IsEditable="True"
                            Text="{Binding Translation, UpdateSourceTrigger=PropertyChanged}"
                            IsKeyboardFocusWithinChanged="ComboBox_IsKeyboardFocusWithinChanged"
                            Style="{DynamicResource {x:Static styles:ResourceKeys.ComboBoxStyle}}">
                    <ComboBox.ItemTemplate>
                      <DataTemplate DataType="infrastructure:ITranslationMatch">
                        <TextBlock>
                          <Span Foreground="Gray">
                            <Run Text="{Binding Rating, StringFormat=F2, Mode=OneWay}" />
                            <Run Text="{Binding Translator.DisplayName, Mode=OneWay}" />:
                          </Span>
                          <Run Text="{Binding TranslatedText, Mode=OneWay}" />
                        </TextBlock>
                      </DataTemplate>
                    </ComboBox.ItemTemplate>
                  </ComboBox>
                </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
          </DataGrid.Columns>
        </DataGrid>
      </DockPanel>
      <toms:LoadingIndicator IsActive="{Binding TranslationSession.IsActive}" Header="Translating..." d:IsHidden="True" />
    </Grid>
  </DockPanel>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
      <i:InvokeCommandAction Command="{Binding StartCommand}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</UserControl>